package com.example.vaccine.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.vaccine.VO.PageVO;
import com.example.vaccine.VO.RecordVO;
import com.example.vaccine.entity.CheckTasks;
import com.example.vaccine.entity.Vaccines;
import com.example.vaccine.form.CreateForm;
import com.example.vaccine.form.CreateVaccineForm;
import com.example.vaccine.mapper.CheckTasksMapper;
import com.example.vaccine.mapper.VaccinesMapper;
import com.example.vaccine.service.VaccinesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.vaccine.util.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Coisn
 * @since 2025-06-12
 */
@Service
public class VaccinesServiceImpl extends ServiceImpl<VaccinesMapper, Vaccines> implements VaccinesService {


    @Autowired
    private VaccinesMapper vaccinesMapper;

    @Autowired
    private CheckTasksMapper checkTasksMapper;
    @Override
    public Boolean create(CreateVaccineForm createVaccineForm) {
        Vaccines vaccine = new Vaccines();

        vaccine.setName(createVaccineForm.getName());
        vaccine.setNumbering(RandomUtil.createRandom());
        vaccine.setAmount(createVaccineForm.getAmount());
        vaccine.setProductionDate(createVaccineForm.getProductionDate());
        vaccine.setInactivationDate(createVaccineForm.getInactivationDate());
        vaccine.setNextCheckDate(createVaccineForm.getNextCheckDate());
        vaccine.setFlag(createVaccineForm.getFlag());
        vaccine.setRegistrantId(createVaccineForm.getRegistrantId());
        Date now = new Date();
        vaccine.setRegisteredAt(now);
        vaccine.setCreatedAt(now);

        int insert = this.vaccinesMapper.insert(vaccine);

        return insert == 1;
    }


    @Override
    public List<RecordVO> singleVaccine(String numbering) {
        if (numbering == null || numbering.isEmpty()) {
            return Collections.emptyList();
        }

        QueryWrapper<Vaccines> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("numbering", numbering);

        Vaccines vaccine = vaccinesMapper.selectOne(queryWrapper);
        if (vaccine == null) {
            return Collections.emptyList();
        }

        RecordVO vo = new RecordVO();
        vo.setId(vaccine.getId());
        vo.setName(vaccine.getName());
        vo.setNumbering(vaccine.getNumbering());
        vo.setAmount(vaccine.getAmount());
        vo.setProductionDate(vaccine.getProductionDate());
        vo.setInactivationDate(vaccine.getInactivationDate());
        vo.setNextCheckDate(vaccine.getNextCheckDate());
        vo.setFlag(vaccine.getFlag());
        vo.setRegistrantId(vaccine.getRegistrantId());
        vo.setRegisteredAt(vaccine.getRegisteredAt());
        vo.setCreatedAt(vaccine.getCreatedAt());

        return Collections.singletonList(vo);
    }



    @Override
    public PageVO vaccineList(Integer page, Integer size) {
        if (page == null || page < 1) page = 1;
        if (size == null || size < 1) size = 10;

        // MyBatis-Plus分页对象
        Page<Vaccines> pageInfo = new Page<>(page, size);

        // 创建查询条件（这里示例不加额外条件，后续可以根据需求加）
        QueryWrapper<Vaccines> queryWrapper = new QueryWrapper<>();

        // 分页查询
        Page<Vaccines> resultPage = vaccinesMapper.selectPage(pageInfo, queryWrapper);

        // 转换为VO列表
        List<RecordVO> voList = new ArrayList<>();
        for (Vaccines vaccine : resultPage.getRecords()) {
            RecordVO vo = new RecordVO();
            vo.setId(vaccine.getId());
            vo.setName(vaccine.getName());
            vo.setNumbering(vaccine.getNumbering());
            vo.setAmount(vaccine.getAmount());
            vo.setProductionDate(vaccine.getProductionDate());
            vo.setInactivationDate(vaccine.getInactivationDate());
            vo.setNextCheckDate(vaccine.getNextCheckDate());
            vo.setFlag(vaccine.getFlag());
            vo.setRegistrantId(vaccine.getRegistrantId());
            vo.setRegisteredAt(vaccine.getRegisteredAt());
            vo.setCreatedAt(vaccine.getCreatedAt());

            // 其他字段根据需要赋值
            voList.add(vo);
        }

        PageVO pageVO = new PageVO();
        pageVO.setData(voList);
        pageVO.setTotal(resultPage.getTotal());

        return pageVO;
    }


    @Transactional
    @Override
    public Boolean delte(Integer id) {
        // 先查出该疫苗的编号
        Vaccines vaccine = this.getById(id);
        if (vaccine == null) {
            return false;  // 无此疫苗，删除失败
        }
        String numbering = vaccine.getNumbering();

        // 删除核查任务中所有关联该疫苗编号的记录
        QueryWrapper<CheckTasks> taskWrapper = new QueryWrapper<>();
        taskWrapper.eq("vaccine_numbering", numbering);
        checkTasksMapper.delete(taskWrapper);

        // 这里如果有其他关联表(如check_records,vaccine_usage,vaccine_feedback)也要类似处理，视需求而定

        // 删除疫苗
        boolean removed = this.removeById(id);
        return removed;
    }


    @Override
    public Boolean update(CreateVaccineForm createVaccineForm) {
        if (createVaccineForm == null || createVaccineForm.getNumbering() == null) {
            return false;
        }

        QueryWrapper<Vaccines> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("numbering", createVaccineForm.getNumbering());

        Vaccines vaccine = vaccinesMapper.selectOne(queryWrapper);
        if (vaccine == null) {
            return false; // 找不到该编号的疫苗
        }

        // 更新字段（除了编号，不可修改）
        vaccine.setName(createVaccineForm.getName());
        vaccine.setAmount(createVaccineForm.getAmount());
        vaccine.setProductionDate(createVaccineForm.getProductionDate());
        vaccine.setInactivationDate(createVaccineForm.getInactivationDate());
        vaccine.setNextCheckDate(createVaccineForm.getNextCheckDate());
        vaccine.setFlag(createVaccineForm.getFlag());
        vaccine.setRegistrantId(createVaccineForm.getRegistrantId());
        vaccine.setRegisteredAt(createVaccineForm.getRegisteredAt());
        vaccine.setCreatedAt(createVaccineForm.getCreatedAt());

        int update = vaccinesMapper.updateById(vaccine);
        return update == 1;
    }

    @Override
    public Vaccines getByNumbering(String numbering) {
        return this.getOne(new QueryWrapper<Vaccines>().eq("numbering", numbering));
    }


}
